package br.urca.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.servlet.ServletContext;

import br.com.caelum.vraptor.Controller;
import br.com.caelum.vraptor.Get;
import br.com.caelum.vraptor.Path;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.jasperreports.Report;
import br.com.caelum.vraptor.jasperreports.download.ReportDownload;
import br.com.caelum.vraptor.jasperreports.formats.ExportFormats;
import br.com.caelum.vraptor.observer.download.Download;
import br.urca.dao.BiReservasDAO;
import br.urca.interceptor.InterceptResource;
import br.urca.model.BiItensReserva;
import br.urca.model.BiReservas;
import br.urca.model.auxiliar.Comprovante;
import br.urca.report.AbstractReport;
import br.urca.session.UsuarioLogado;
import br.urca.util.CryptDoc;

@Controller
@InterceptResource
public class ReservasController {
	
	private Result result;
	private UsuarioLogado usuario;
	private BiReservasDAO mBiReservasDAO;	
	private ServletContext context;
	
	@Deprecated public ReservasController() { }

	@Inject
	public ReservasController(Result result, UsuarioLogado usuario,
			BiReservasDAO mBiReservasDAO, ServletContext context) {
		super();
		this.result = result;
		this.usuario = usuario;
		this.mBiReservasDAO = mBiReservasDAO;
		this.context = context;
	}
	
	@Path("/minhas.reservas")
	public void index() { }
	
	@PostConstruct
	public void buscarReservas() {
		List<BiReservas> mList = null;
		List<BiItensReserva> itensReservaList = new ArrayList<BiItensReserva>();
		if (usuario.getFuncionario() != null) {
			mList = mBiReservasDAO.getListPorFuncionario(usuario.getFuncionario().getMatricula());
		} else if (usuario.getAlunoCurso() != null) {
			mList = mBiReservasDAO.getListPorAluno(usuario.getAlunoCurso().getMatricula());
		}
		
		if (mList != null && !mList.isEmpty()) {
			for (BiReservas br: mList) {
				itensReservaList.addAll(br.getBiItensReservaList());
			}
			
			for(BiItensReserva item: itensReservaList) {
				item.setCodReservaCript(CryptDoc.encrypt(item.getBiItensReservaPK().getCodReserva()));
			}
			
			result.include("listaItensReserva", itensReservaList);
		} else {
			result.include("mensagem", "Nenhuma reserva encontrada para você!");
		}
		
	}
	
	@Get("/minhas.reservas/comprovante.pdf")
	public Download comprovante(String codReserva) {
		String codigoReserva = CryptDoc.decrypt(codReserva);
		BiReservas reserva = mBiReservasDAO.getPorCodigoReserva(codigoReserva);
		if (reserva != null) {
			Comprovante comprovante = new Comprovante();			
			SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss");
			if (usuario.getFuncionario() != null) {
				comprovante.setAluno(usuario.getFuncionario().getMatricula() + " - " +usuario.getFuncionario().getNome());
			} else if (usuario.getAlunoCurso() != null) {
				comprovante.setAluno(usuario.getAlunoCurso().getMatricula() + " - " +usuario.getAlunoCurso().getAluno().getNome());
			}
			comprovante.setNumeroReserva(reserva.getCodReserva());
			comprovante.setDataReserva(sdf.format(reserva.getData()));
			
			if (reserva.getBiItensReservaList().get(0).getBiMateriais() != null) {
				comprovante.setCodReg(String.valueOf(reserva
						.getBiItensReservaList().get(0).getBiMateriais()
						.getBiMateriaisPK().getCodNumeroMaterial()));
				comprovante.setTitulo(reserva
						.getBiItensReservaList().get(0).getBiMateriais().getTitulo());
				comprovante.setTipo(reserva
						.getBiItensReservaList().get(0).getBiMateriais().getBiMateriaisPK().toString());
				comprovante.setAno(String.valueOf(reserva
						.getBiItensReservaList().get(0).getBiMateriais().getAnoPublicacao()));
			} else if (reserva.getBiItensReservaList().get(0).getBiCopias() != null) {
				comprovante.setCodReg(String.valueOf(reserva
						.getBiItensReservaList().get(0).getBiCopias()
						.getBiMateriais().getBiMateriaisPK()
						.getCodNumeroMaterial()));
				comprovante.setTitulo(reserva
						.getBiItensReservaList().get(0).getBiCopias().getBiMateriais().getTitulo());
				comprovante.setTipo(reserva
						.getBiItensReservaList().get(0).getBiCopias().getBiMateriais().getBiMateriaisPK().toString());
				comprovante.setAno(String.valueOf(reserva
						.getBiItensReservaList().get(0).getBiCopias().getAno()));
			} else {
				comprovante.setCodReg("");
				comprovante.setTitulo("");
				comprovante.setTipo("");
				comprovante.setAno("");
			}
			
			List<Comprovante> mList = new ArrayList<Comprovante>();
			mList.add(comprovante);
			
			Report report = new AbstractReport(mList, "comp_soli_reserva.jasper", "comp_soli_reserva_");
			String realPath = context.getRealPath("/WEB-INF/reports/images/");	
			report.addParameter("realPath", realPath);
			
			return new ReportDownload(report, ExportFormats.pdf(), false);
			
		} else {
			result.include("mensagem", "Reserva não encontrada!").redirectTo(this).index();
		}
		
		return null;
	}

}
